﻿using Biz;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace App
{
    public partial class FrmNhapHang : Form
    {
        public FrmNhapHang()
        {
            InitializeComponent();
        }
        Product product = new Product();
        List<Product> products;
        List<ReceiptDetail> receipts = new List<ReceiptDetail>();
        private void FrmNhapHang_Load(object sender, EventArgs e)
        {
            products = product.GetAll();
            LoadDsSanPham();
            //LoadDsNhap();
        }

        public void LoadDsSanPham()
        {
            lstDsSanPham.Items.Clear();
            foreach (var p in products)
            {
                lstDsSanPham.Items.Add(p);
            }
        }
        public void LoadDsNhap()
        {
            lstDsNhap.Items.Clear();
            if (receipts.Count >= 0)
            {
                foreach (var r in receipts)
                {
                    ListViewItem lvi = new ListViewItem(r.Product.Id.ToString());
                    ListViewItem.ListViewSubItem sit = new ListViewItem.ListViewSubItem(lvi, r.Product.Name);
                    ListViewItem.ListViewSubItem sit1 = new ListViewItem.ListViewSubItem(lvi, r.Quantity.ToString());
                    ListViewItem.ListViewSubItem sit2 = new ListViewItem.ListViewSubItem(lvi, r.Product.CurrentPrice.ToString());
                    lvi.SubItems.Add(sit);
                    lvi.SubItems.Add(sit2);
                    lvi.SubItems.Add(sit1);
                    lvi.Tag = r;
                    lstDsNhap.Items.Add(lvi);
                }

            }
        }
        private void btnNhap_Click(object sender, EventArgs e)
        {
            if (lstDsSanPham.SelectedIndex<0)
            {
                MessageBox.Show(this, "Chưa chọn sản phẩm!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (String.IsNullOrEmpty(txtQuantity.Text))
            {
                MessageBox.Show(this, "Chưa nhập số lượng!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Product product = (Product)lstDsSanPham.SelectedItem;
            var r = receipts.SingleOrDefault(p => p.Product == product);
            if (r != null)
            {
                r.Quantity = int.Parse(txtQuantity.Text);
            }
            else
            {
                ReceiptDetail detail = new ReceiptDetail();
                detail.Product = product;
                detail.Price = product.CurrentPrice;
                detail.Quantity = int.Parse(txtQuantity.Text);
                receipts.Add(detail);
            }
            txtQuantity.Text = "";
            LoadDsNhap();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ReceiptNote receipt = new ReceiptNote();
            receipt.Time = DateTime.Now;
            receipt.Insert(receipts);
            MessageBox.Show(this, "Nhập hàng thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            lstDsNhap.Items.Clear();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (lstDsNhap.SelectedItems.Count <= 0)
            {
                MessageBox.Show(this, "Chưa chọn dòng cần xóa!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            ReceiptDetail detail = (ReceiptDetail)lstDsNhap.SelectedItems[0].Tag;
            receipts.Remove(detail);
            LoadDsNhap();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            lstDsNhap.Items.Clear();
        }

        private void txtTimNhanh_TextChanged(object sender, EventArgs e)
        {
            products = product.SearchByCondition(txtTimNhanh.Text, -1, -1, -1, -1);
            LoadDsSanPham();
        }
    }
}
